#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <sqlite3.h>


int main(int argc, char *argv[])
{
	int cfd;
	struct sockaddr_in srv_addr;

	srv_addr.sin_family = AF_INET;
	srv_addr.sin_port = htons(8888);
	srv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");

	//1.创建套接字
    cfd = socket(AF_INET , SOCK_STREAM,0);
    if(cfd == -1)
    {
        perror("socket");
        return -1;
    }
	//２.主动连接
	if( connect(cfd, (struct sockaddr*)&srv_addr, sizeof(srv_addr)) == -1)
	{
		perror("connect");
		return -1;
	}
	printf("连接成功，请输入发送信息：\n");
	while (1) 
	{
		int n;
		printf("输入0为登陆，输入１为注册\n");
		scanf("%d", &n);
		if(n==0)
		{
			char buf[20] = "login";
			char name[20];
			char passwd[20];
			write(cfd, buf, sizeof(buf));
			printf("输入账户:");
			scanf("%s", name);
			printf("输入密码:");
			scanf("%s", passwd);
			write(cfd, name, sizeof(name));
			write(cfd, passwd, sizeof(passwd));
			printf("姓名：%s密码：%s",name,passwd);
			read(cfd, buf, sizeof(buf));
			printf("结果是:%s\n",buf);
		}
		else
		{
			char buf[20] = "register";
			char name[20];
			char passwd[20];
			write(cfd, buf, sizeof(buf));
			printf("输入账户:");
			scanf("%s", name);
			printf("输入密码:");
			scanf("%s", passwd);
			write(cfd, name, sizeof(name));
			write(cfd, passwd, sizeof(passwd));
			read(cfd, buf, sizeof(buf));
			printf("结果是:%s\n",buf);
		}
	}

}

